﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNet.SignalR;
using Microsoft.AspNet.SignalR.Hubs;
using System.Threading.Tasks;
using System.Web;
using Youz.CreditRepay.BLL;
using Youz.CreditRepay.Entity;
using Youz.Common.BLL;

namespace Youz.CreditRepay.SignalR
{
    public class AuthenticatedConnection : PersistentConnection
    {
        protected override bool AuthorizeRequest(IRequest request)
        {
            return request.User != null && request.User.Identity.IsAuthenticated;
        }
    } 

    [HubName("TickerHub")]
    [Authorize]
    public class TickerHub : Hub
    {
        private readonly Ticker _stockTicker;
        private static ConnectionMapping<string> _connections;

        public TickerHub() :
            this(Ticker.Instance)
        {
            _connections = Ticker.Connections;
        }

        public TickerHub(Ticker stockTicker)
        {
            _stockTicker = stockTicker;
        }

        public string Test(string name)
        {
            return "hello!";
        }
                
        public override Task OnConnected()
        {
            // Add your own code here.
            // For example: in a chat application, record the association between
            // the current connection ID and user name, and mark the user as online.
            // After the code in this method completes, the client is informed that
            // the connection is established; for example, in a JavaScript client,
            // the start().done callback is executed.
            string name = Core.MemberAccount;

            if (!string.IsNullOrEmpty(name))
            {
                _connections.Add(name, Context.ConnectionId);
            }

            var group = Context.QueryString["Group"];
            Groups.Add(Context.ConnectionId, string.IsNullOrEmpty(group) ? ClientGroup.Default.ToString() : group);            

            return base.OnConnected();            
        }
        
        public override Task OnDisconnected(bool stopCalled)
        {
            // Add your own code here.
            // For example: in a chat application, mark the user as offline, 
            // delete the association between the current connection id and user name.
            string name = Core.MemberAccount;

            if (!string.IsNullOrEmpty(name)) _connections.Remove(name, Context.ConnectionId);

            var group = Context.QueryString["Group"];
            Groups.Remove(Context.ConnectionId, string.IsNullOrEmpty(group) ? ClientGroup.Default.ToString() : group); 

            return base.OnDisconnected(stopCalled);
        }

        public override Task OnReconnected()
        {
            // Add your own code here.
            // For example: in a chat application, you might have marked the
            // user as offline after a period of inactivity; in that case 
            // mark the user as online again.
            string name = Core.MemberAccount;

            if (!string.IsNullOrEmpty(name) && !_connections.GetConnections(name).Contains(Context.ConnectionId))
            {
                _connections.Add(name, Context.ConnectionId);
            }

            return base.OnReconnected();
        }
    }
}